define([ 'jquery', 'jquery_route', 'arttemplateUtil' ,'common','config','pageJs','filer','validform','wdatePicker' ], function($,
		jquery_route, arttemplateUtil,common,config,pageJs,filer) {
	
	$.controller("controller.market.activitycoupon", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		
		$model($defobj);
		
		var coupon_type = $paramobj.coupon_type == undefined ? 51 : $paramobj.coupon_type;
		var pageNo = $paramobj.page == undefined ? 1 : $paramobj.page;
		//判断是否有传tab参数，有传的话，根据tab的标签值对应标签设置为选中状态
		if(coupon_type != undefined) {
			$('#tabs-632335 li').each(function(){
				if($(this).find('a').attr("tab_value") == coupon_type) {
					$(this).addClass('active');
				} else{
					$(this).removeClass('active');
				}
			});
		}
		
		//coupon_activity_type:优惠券活动类型[51:满减优惠券；52：企业团购抵用券;53：商品特价优惠券；] 
		var param = {coupon_activity_type:coupon_type,pageNo:pageNo}; 
		//站点管理列表模板
		var $activity_coupon_list_tpl;
		
		var initData = function(){
			var index = common.loading();
			common.common_ajax_request('/api/seller/v1/queryActivityCouponList',param,false,function(data,code,msg){
				if(msg.businessCode==0){
					setData(data);
					pageJs.setPage(data.pageNo,data.totalPage,data.totalCount,callback);			
				}else{
					setData({});
					window.layer.msg("系统异常");
				}
				layer.close(index); //关闭加载层
			},"","post");
		}
		
		var initTpl = function(coupon_activity_type){
			//满减优惠券模板
			var activityCouponHtmlContent = 'market/templates/full_reduction_coupon_tpl.html';
			
			if(coupon_activity_type == 53) {//商品特价优惠券活动模板
				activityCouponHtmlContent = 'market/templates/special_offer_of_goods_coupon_tpl.html';
			} else if(coupon_activity_type == 52) {//企业团购抵用券模板
				activityCouponHtmlContent = 'market/templates/enterprise_group_purchase_coupon_tpl.html';
			}
				
			//根据选择的标签加载对应的页面模板
			$activity_coupon_list_tpl = arttemplateUtil.template.compile($.setHTML(activityCouponHtmlContent));
		}
		
		var setData = function(data){
			var res = {
					records:data.records,
					pageNo: data.pageNo
			}
			$("#coupon_list").html($activity_coupon_list_tpl(res));
		}
		
		var callback = function(page){
			param.pageNo=page;
			initData();
		}
		
		var init = function() {
			initTpl(coupon_type);
			initData();
		};
		
		//页签选择,更换页签需要清空查询条件
		$('#tabs-632335 a').click(function (e) {
			  var tab_value = $(this).attr("tab_value");
			  param.coupon_activity_type=tab_value;
			  initTpl(param.coupon_activity_type);
			  param.pageNo=1;
			  initData();
		});
		
		//查询
	   $('.serchBtn').on('click',function(){
		   param.pageNo=1;
		   $.each($('#serchForm').serializeArray(), function(i, item) {
				if(item.value==""){
					delete param[item.name]
				}else{
					param[item.name]=item.value;
				}
			})
			initData();
		});
		   
		//重置
		$('.resetBtn').on('click',function(){
			$.each($('#serchForm').serializeArray(), function(i, item) {
				delete param[item.name];
			});
			$("form[name=serchForm]")[0].reset()
			initData();
		});
		
		init();
		
		//新增
		$('.editBtn').on('click',function(){
			location.href='#market/activitycoupon/edit//'+ param.coupon_activity_type + '/' + param.pageNo;
		});
		
		
		//失效
		$('#coupon_list').off('click','.disabledCoupon').on('click','.disabledCoupon',function(){
			var obj = this;
			layer.confirm('确定要执行该操作？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
					common.common_ajax_request('/api/seller/v1/activityCouponDisabled',{coupon_activity_id:$(obj).attr("data-id")},false,function(data,code,msg){
						if(msg.businessCode==0 && data==true){
							window.layer.msg("操作成功！");
							$(obj).parent().find('.editCoupon').remove();
							$(obj).parents('.con_detail_cls').find('.act_status_cls')
								.html('已失效').removeClass().addClass('f-gray act_status_cls');
							$(obj).remove();
							
						} else {
							window.layer.msg('操作失败')
						}
					},"","get");
				}, function(){
				  
			});
		});
		
		//鼠标经过移开
		$(document).off('mouseenter','.zdyhbox').on('mouseenter','.zdyhbox',function(){
			$(this).popover('show');
		});
		$(document).off('mouseleave','.zdyhbox').on('mouseleave','.zdyhbox',function(){
			$(this).popover('hide');
		});
		
		/********************************************查看优惠券相关处理开始***********************************************/
		var $detail_param={};
		
		var app_couponDetail = {
			name:'兑换券详情列表',
			//加载优惠券弹窗模板
			addCouponDetailTpl: function() {
				//判断是否存在已领取优惠券列表信息，有的话弹出窗口，反之提示
				common.common_ajax_request('/api/seller/v1/queryCouponDetailByActId',$detail_param,false,function($data,$code,msg){
					if($code==200){
						//判断是否存在已领取优惠券列表信息，有的话弹出窗口，反之提示
						if($data.totalCount > 0) {
							$('#coupon_modal').modal('show');
							$('#coupon_content').html(arttemplateUtil.template.compile($.setHTML('market/templates/activity_coupon_view_tpl.html'))({coupon_activity_type:param.coupon_activity_type,coupon_name:$detail_param.coupon_name}));
							app_couponDetail.renderCouponDetailList($data);
						} else {
							window.layer.msg('没有已领取的优惠券');
						}
					}
				},"","get");
			},
			//兑换券分页
			pagefun: function(page){
				//分页回调
				$detail_param.pageNo=page;
				app_couponDetail.initCouponDetail();
			},
			initCouponDetail : function() {
				
				common.common_ajax_request('/api/seller/v1/queryCouponDetailByActId',$detail_param,false,function($data,$code,msg){
					if($code==200){
						app_couponDetail.renderCouponDetailList($data);
					}
				},"","get");
			},
			renderCouponDetailList: function($data) {
				$defobj.data = $data;
				$defobj.coupon_activity_type = param.coupon_activity_type;
				//加载优惠券列表信息
				$('#exchange_list').html(arttemplateUtil.template.compile($.setHTML('market/templates/activity_coupon_view_list_tpl.html'))($defobj));
				pageJs.setPage_dynamic(
						{pageBox:'#view_pageBox',records:'#view_records',page:'#pagination'},
						$data.pageNo,$data.totalPage,$data.totalCount,app_couponDetail.pagefun);
			}
		}
		
		//点击查看优惠券
		$('#coupon_list').off('click','.viewCoupon').on('click','.viewCoupon',function(){
			var coupon_name = $(this).attr("data-name");
			var id = $(this).attr("data-id");
			
			$detail_param.coupon_name = coupon_name;
			$detail_param.coupon_activity_id = id;
			$detail_param.pageNo = 1;
			//初始化优惠券并打开窗口
			app_couponDetail.addCouponDetailTpl();
			
			
			//导出优惠券列表信息
			$('#coupon_content').off('click','.exportBtn').on('click','.exportBtn',function(){
				$("#excel").attr("action",config.apiRoot()+"/api/seller/v1/exportCouponDetail");
				var html = [];
				html.push("<input name=\"coupon_activity_id\" type='hidden' id=\"coupon_activity_id\" value=\""+id+"\" />");
				html.push("<input name=\"coupon_name\" type='hidden' id=\"coupon_name\" value=\""+coupon_name+"\" />");
				html.push("<input name=\"coupon_activity_type\" type='hidden' id=\"coupon_activity_type\" value=\""+param.coupon_activity_type+"\" />");
				$.each(config.setFullParameterArr({}), function(i, item) {
					html.push("<input name=\""+i+"\" type='hidden' id="+i+" value=\""+item+"\" />");
				});
				$("#excel").html(html.join(""));
				$("#excel").submit();
				
			});
			
		});
		/********************************************查看优惠券相关处理结束***********************************************/
		
	});

	var App={
			name:'活动兑换券管理'
	};
});